package com.srcbox.file.adapter

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.fastjson.JSONArray
import com.srcbox.file.R
import com.srcbox.file.data.CapturedImageConfig
import com.srcbox.file.data.ScreenCaptureInfo
import com.srcbox.file.service.CapturedImagesServer
import com.srcbox.file.util.EggUtil
import com.srcbox.file.util.ScreenCaptureUtil

class LocalFunList(
    private val context: Activity,
    private val appFunJson: JSONArray,
    val v: View
) :
    RecyclerView.Adapter<RecyclerView.ViewHolder>() {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        val view = LayoutInflater.from(context).inflate(R.layout.local_fun_list, parent, false)
        return MeHolder(view)
    }

    override fun getItemCount(): Int {
        return appFunJson.size
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        if (holder is MeHolder) {
            val jsonA = appFunJson.getJSONArray(position)
            val name = jsonA[0].toString()
            val icon = jsonA[1].toString()
            holder.bind(name, icon)
        }
    }

    inner class MeHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        fun bind(name: String, icon: String) {
            itemView.findViewById<TextView>(R.id.localTitle).text = name
            EggUtil.setViewIcon<ImageView>(
                context,
                itemView.findViewById(R.id.localIcon),
                icon
            )

            itemView.setOnClickListener {
                when (name) {
                    "屏幕资源提取" -> {
                        (this@LocalFunList.context as Context).startService(
                            Intent(
                                this@LocalFunList.context,
                                CapturedImagesServer::class.java
                            )
                        )

//                        EggUtil.toast(this@LocalFunList.context, "${ScreenCaptureInfo.isStart}")

                        if (!ScreenCaptureInfo.isStart) {
                            if (!CapturedImageConfig.isServerPermission) {
                                ScreenCaptureUtil.initAccessibilityPermissions(context)
                                return@setOnClickListener
                            }

                            if (CapturedImageConfig.isServerStart) {
                                ScreenCaptureUtil.readyCapture(
                                    context,
                                    ScreenCaptureInfo.CODE
                                )
                            } else {
                                EggUtil.toast(this@LocalFunList.context, "服务未启动")
                                ScreenCaptureUtil.initAccessibilityPermissions(context)
                            }
                        }
                    }

                    "游戏资源提取" -> {
                        EggUtil.toast(context, "敬请期待")
                    }
                }
            }
        }
    }
}


/*if (!ScreenCaptureInfo.isStart) {
                   if (!ScreenCaptureInfo.isStart) {
                       if (!ScreenCaptureUtil.initAccessibilityPermissions(context)) {
                           return@setOnClickListener
                       }
                   }


//                            FloatWin(context).show()
               }*/


/*



val bottomSheet = v.findViewById<CardView>(R.id.design_bottom_sheet1)
                        val bsb = BottomSheetBehavior.from(bottomSheet)
                        bsb.state = BottomSheetBehavior.STATE_COLLAPSED



val rootDataDir =
                        "${Environment.getExternalStorageDirectory()}/Android/data"
                    val rddO = File(rootDataDir)

                    if (rddO.isDirectory) {
                        val listFiles = rddO.listFiles(FileFilter {
                            it.isDirectory || it.name[0] != '.'
                        })
                        val applicationInfoList = ArrayList<ApplicationInfo>()
                        Thread {
                            listFiles?.forEach {
                                try {
                                    applicationInfoList.add(
                                        context.packageManager.getApplicationInfo(
                                            it.name,
                                            0
                                        )
                                    )
                                } catch (e: PackageManager.NameNotFoundException) {
                                    println(e.message)
                                }
                            }

                        }.start()
                        context.runOnUiThread {
                            val localAppSrcListV = v.findViewById<RecyclerView>(R.id.local_src_list)
                            localAppSrcListV.layoutManager = GridLayoutManager(context, 2)
                            localAppSrcListV.adapter = LocalAppSrc(context, applicationInfoList)
                        }
                    } else {
                        EggUtil.toast(context, "错误")
                    }*/